package com.sjm.suchbear.minibookmall.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.BookDB;
import com.sjm.suchbear.minibookmall.BookDetailActivity;
import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.BookManage;
import com.sjm.suchbear.minibookmall.fragment.adapter.BookListAdapter;
import com.sjm.suchbear.minibookmall.model.BookModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class BookFragment extends Fragment {


    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.et_searchKeyWord)
    EditText etSearchKeyWord;
    @BindView(R.id.iv_searchBook)
    ImageView ivSearchBook;
    @BindView(R.id.rl_search)
    LinearLayout rlSearch;
    @BindView(R.id.lv_bookList)
    MyListView lvBookList;
    Unbinder unbinder;

    private List<BookModel> books;
    private SQLiteDatabase dbread;
    private BookListAdapter bookListAdapter;
    private List<BookModel> searchs;
    private boolean isSearching = false;

    public BookFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book, container, false);
        unbinder = ButterKnife.bind(this, view);
        llHeaderBack.setVisibility(View.GONE);
        tvListTitle.setText("书城");
        BookDB db = new BookDB(getActivity());
        dbread = db.getReadableDatabase();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        books = BookManage.getBookList(dbread);
        bookListAdapter = new BookListAdapter(getActivity(), books);
        lvBookList.setAdapter(bookListAdapter);
        lvBookList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent detailIntent = new Intent(getActivity(), BookDetailActivity.class);
                if (isSearching) {
                    detailIntent.putExtra("bookID", searchs.get(i).getBookID());
                } else {
                    detailIntent.putExtra("bookID", books.get(i).getBookID());
                }
                startActivity(detailIntent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.iv_searchBook)
    public void onViewClicked() {
        String keyword = String.valueOf(etSearchKeyWord.getText()).trim();
        if (!keyword.equals("")) {
            isSearching = true;
            searchs = new ArrayList<>();
            for (int i = 0; i < books.size(); i++) {
                if (books.get(i).getBookname().contains(keyword)) {
                    searchs.add(books.get(i));
                }
            }
            bookListAdapter = new BookListAdapter(getActivity(), searchs);
            lvBookList.setAdapter(bookListAdapter);
            bookListAdapter.notifyDataSetChanged();
        } else {
            isSearching = false;
            bookListAdapter = new BookListAdapter(getActivity(), books);
            lvBookList.setAdapter(bookListAdapter);
            bookListAdapter.notifyDataSetChanged();
        }
    }
}
